<template>
  <div class="my-bootm">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>


      <el-form-item label="密码" prop="rpwd">
        <el-input type="password" v-model="ruleForm.rpwd"></el-input>
      </el-form-item>

      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-select v-model="ruleForm.sex" placeholder="请选择性别" class="my-put">
          <el-option
            v-for="(item,index) in optionsAccount"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="年龄" prop="age">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="ruleForm.status" placeholder="请选择状态" class="my-put">
          <el-option
            v-for="(item,index) in optionsCommon"
            :key="index"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="个人头像" prop="head_img">
        <!--<el-input v-model="ruleForm.head_img"></el-input>-->

        <el-upload
          action="http://127.0.0.1:8111/"
          list-type="picture-card"
          :auto-upload="false"
          :on-change="OnChange"
          :on-remove="OnRemove"
          :on-preview="handlePictureCardPreview"
          :before-remove="beforeRemove"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm()">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>
<style>
  .my-bootm{
    background-color: #FFFFFF;
    padding: 20px;
  }
  .my-put{
    width:100%;
  }
</style>
<script>
  export default {
    name: 'Personal',
    data() {
      return {
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        optionsAccount: [],  // 存储CommonChoices实际数据的列表
        EditUrl: this.$http.accountURL,  // get编辑的URL
        EditIdStr: 'account_id',  // 模块id字符串

        ruleForm: {
          username: '', // 用户名
          pwd: '', // 密文密码
          rpwd: '',
          head_img: '', // 个人头像
          name: '', // 姓名
          sex: '', //性别
          age: '', // 年龄
          status: '', // 状态
        },  //
        rules: {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'}
          ],
          pwd: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 0, max: 16, message: '长度在 0 到 16 个字符', trigger: 'blur'}
          ],
          rpwd: [
            {required: false, message: '确认密码', trigger: 'change'},
            {min: 0, max: 16, message: '长度在 0 到 16 个字符', trigger: 'blur'}
          ],
          head_img: [
            {required: false, message: '请输入个人头像', trigger: 'blur'},
          ],
          name: [
            {required: false, message: '姓名', trigger: 'blur'},
          ],

          sex: [
            {required: false, message: '性别', trigger: 'blur'},
          ],
          age: [
            {required: false, message: '年龄', trigger: 'blur'},
          ],

          status: [
            {required: true, message: '请选择状态', trigger: 'change'}
          ],
        },

        param: new FormData(),
        count: 0,
        fileList: [],
        dialogVisible: false,
        dialogImageUrl: ''
      };
    },
    created() {
      this.getEditData();   // get回将要编辑的数据
      this.optionsCommon = this.getChoices.common;
      this.optionsAccount = this.getChoices.account;
    },
    methods: {
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      OnChange(file, fileList) {
        this.fileList = fileList

      },
      OnRemove(file, fileList) {
        this.fileList = fileList
      },

      submitForm() {
        let param= new FormData();
        if (this.ruleForm.rpwd) {
          this.ruleForm.pwd = this.$myTool.pwdMd(this.ruleForm.rpwd);
        }
        if (this.fileList.length) {
          this.ruleForm.head_img = this.fileList[0].raw;
        }else {
          delete(this.ruleForm.head_img);
        }
        for (let x in this.ruleForm) {
          param.append(x, this.ruleForm[x])
        }

        this.$http.getApiFunc(this.$http.updateApiStatus)(this.$http.accountURL, this.$route.query, param)
          .then(res => {
            this.$alert(res.data.msg, '提示', {
              confirmButtonText: '确定'
            });
            if (!res.data.status) {
              localStorage.clear();
              var userInfo = localStorage.getItem("userInfo");
              userInfo = JSON.parse(userInfo);
              this.$store.dispatch('getUserInfo', userInfo);
              this.$router.push({path: '/base'});
            }
          })
          .catch(err => {
            console.log(err)
          });
      },

      // get回将要编辑的数据
      getEditData() {
        this.$myTool.getEditData(this)
      },

      // get将要编辑的数据
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },

    watch: {
      fileList: function (value) {
        if (this.fileList.length > 1) {
          this.fileList.splice(this.fileList.length - 1, 1);
          this.$alert('只允许存在一个文件', '提示', {
            confirmButtonText: '确定'
          });
        }
      },
    },

    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },
  }
</script>
